<?php

namespace controller;

use service\api\DidCheck;
use service\Cipher;
use think\Controller;
use think\facade\Log;

class BasicApi extends Controller
{
    protected $sign = true;
    protected $v = 0;
    protected $app_v = 0;
    protected $app_no = 0;
    protected $auth_type = 3;
    protected $did_info = null;
    protected $uid_info = null;
    public    $_time;
    /**
     * @param int $auth_type 认证类型配置
     * @example 1.开启设备认证  2.开启用户登录验证 3.同时开启设备和用户登录验证 4.关闭设备和用户登录验证
     */
    public function initialize()
    {
        if($this->auth_type == 1 || $this->auth_type == 3 ) {
            $this->did_info = DidCheck::checkAuth($this->request);
        }

        if($this->auth_type == 2 || $this->auth_type == 3 ) {
            $this->uid_info = DidCheck::checkUserAuth($this->request);
        }
//        if($this->request->param('auth_did') != 1 || env('app_valide')) {
//
//        }else{
//            $this->did_info = DidCheck::checkAuth($this->request);
//            if(!in_array(request()->controller(), ['Login', 'Store', 'Banner', 'Sns', 'Game'])) {
//                $this->uid_info = DidCheck::checkUserAuth($this->request);
//            }
//        }
        $this->_time = time();
        $this->v = $this->request->v;                  //服务端版本号
        $this->app_v = $this->request->app_v;          //APP版本号
        $this->app_no = $this->request->app_no;          //APP版本号
    }

    /**
     * 返回成功信息   并做日志记录  （在并发较高时，应关掉日志读写）
     * @param array $detail         返回数据
     * @param string $msg           提示信息
     * @param bool $filter_arr      是否过滤数据
     */
    public function successView($detail = array(), $msg = '' ,$filter_arr = true)
    {
        $res_str = "";
        if (!empty($detail)) {
            if ($filter_arr == true) {
                array_walk_recursive($detail, function (&$v) {
                    if (is_numeric($v))
                        $v = floatval($v);
                    else if (is_null($v))
                        $v = '';
                    else if (is_bool($v))
                        $v = intval($v);
                });
            }
            if ($this->sign && env('APP_ENV') != 'debug') {
                $tmp = json_encode($detail);
                Log::record('接口返回参数未加密：'.$tmp);
                $res_str = Cipher::encrypt_openssl($tmp, DidCheck::$device_info->key);
            } else {
                $res_str = $detail ?: '';
            }
        }

        $info['code'] = 1000;
        $info['msg'] = $msg;
        $info['details'] = $res_str;
        Log::record('接口返回参数：'.json_encode($info));
        return json($info);
    }

    /**
     * 返回错误信息   并做日志记录  （在并发较高时，应关掉日志读写）
     * @param $rc               错误码
     * @param $error_message    错误提示
     * @param array $detail     返回数据
     */

    public function errorView($rc, $error_message = '')
    {
        $rc = intval($rc);
        $info['code'] = $rc;
        $info['msg'] = $error_message;
        if(DidCheck::$request != null) {
            Log::write('Path:['.DidCheck::$request->path().']');
            Log::write('Method:['.DidCheck::$request->method().']');
            Log::write('Data:['.json_encode(DidCheck::$request->param()).']');
            Log::write('Key:['.DidCheck::$device_info->key.']');
            Log::write('Error:['. $rc . $error_message.']');
        }
        return json($info);
    }
}
